package com.dhms.vostok1.data

import android.graphics.drawable.Drawable
import com.dhms.vostok1.data.todo.ToDoStatisticsData

data class BaseListItem(
    val itemPrefixIcon: Drawable? = null,
    val itemTitle: String,
    var itemValue: String? = null,
    val itemUnit: String? = null,
    val itemSuffixIcon: Drawable? = null
)

typealias OnBaseListItemClickListener = (position: Int) -> Unit

data class TaskAndDeviceItem(
    val itemId: Long,
    val itemTitle: String,
    val itemTime: String? = null,
    val itemExtraInfo: String? = null,
    val itemImgUrl: String? = null,
    val itemStatus: String? = null,
    val itemStatusColor: Int? = null,
    val itemStatusIsNotVisible: Boolean? = false,
    val itemCheckable: Boolean = true,
    val itemPerilSum: Int? = null,
    val itemFaultSum: Int? = null,
    val deHasSensor: String? = null,
) {
    val deviceHasSensor: Boolean
        get() {
            return deHasSensor?.lowercase() == "yes"
        }
}

class DeviceFaultRecordItem {
    var time: Long? = null

    @Suppress("unused")
    var metricName: String? = null
    var value: String? = null
    var threshold: String? = null
    var level: Int? = null

    companion object {
        @JvmStatic
        fun fillDeviceFaultRecordItem(faOccurredSteps: String): List<DeviceFaultRecordItem> {
            return faOccurredSteps.split("\n").map {
                it.split(",")
            }.filter {
                it.size == 3
            }.map { step ->
                DeviceFaultRecordItem().apply {
                    this.value = step[0]
                    this.level = step[1].toIntOrNull()
                    try {
                        this.time = step[2].toLong()
                    } catch (parseErr: NumberFormatException) {
                        // ignore parse error and keep the time field's value as null
                    }
                    this.threshold = step[1]
                }
            }
        }
    }
}

enum class StatisticsType(val value: Int) {
    LINE_PROGRESS(0),
    CIRCLE_PROGRESS(1);

    companion object {
        fun fromInt(type: Int) =
            StatisticsType.values().associateBy(StatisticsType::value)[type]
                ?: StatisticsType.LINE_PROGRESS
    }
}

data class ToDoStatisticsItem(
    val type: StatisticsType = StatisticsType.LINE_PROGRESS,
    val data: ToDoStatisticsData = ToDoStatisticsData()
)